'use strict'

var Crewler = require('crawler');
var articles = require('../models/articles');
var https = require('https');
var fs = require('fs');

var add = [];

var c = new Crewler({
    maxConnection: 1,
    rateLimit: 10,
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;

            var list = $('.dw_table .el').toArray();

            console.log(list);

            list.forEach(element => {
                //职业名：Positions
                //公司名：CompanyName
                //工作地点：workingPlace
                //工资:pay
                //发布时间：ReleaseTime
                // var title = $(element).find(' .pl2 a').text().trim().replace(/[ \r\n]/g, '');
                var Positions = $(element).find(' .t1 span').text().trim().replace(/[ \r\n]/g, '');
                console.log(Positions);

                var CompanyName = $(element).find(' .t2 a').attr('title');
                console.log(CompanyName);

                var workingPlace = $(element).find(' .t3').text().trim().replace(/[ \r\n]/g, '');
                console.log(workingPlace);

                var pay = $(element).find(' .t4').text().trim().replace(/[ \r\n]/g, '');
                console.log(pay);

                var ReleaseTime = $(element).find(' .t5').text().trim().replace(/[ \r\n]/g, '');
                console.log(ReleaseTime);

                articles.create({ Positions: Positions, CompanyName: CompanyName, workingPlace: workingPlace, pay: pay, ReleaseTime: ReleaseTime })
            })

        }
        done();
    }
})

setTimeout(() => {
    
for(let index = 1;index <= 15;index++){
    c.queue('https://search.51job.com/list/010000%252C180200%252C020000%252C030200%252C040000,000000,7501,00,9,99,%2520,1,'+index+'.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
}

}, 3);



// var url = 'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C180200,000000,0000,00,9,99,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590%25E5%25B8%2588,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=';
// c.queue(url);

module.exports = c;